package com.microstation.service.impl;

import java.io.File;
import java.util.List;
import java.util.UUID;

import com.microstation.common.AppConst;
import com.microstation.entity.vo.UserInfoVO;
import com.microstation.enums.ResultEnum;
import com.microstation.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.microstation.service.UserInfoService;
import com.microstation.dao.UserInfoDao;
import com.microstation.common.Assist;
import com.microstation.common.Result;
import com.microstation.entity.po.UserInfo;
import org.springframework.transaction.annotation.Transactional;

/**
 * UserInfo的服务接口的实现类
 *
 * @author
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private OSSFilesUtil ossFilesUtil;

    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public Result find(Assist assist) {
        //TODO这里可以做通过Assist做添加查询
        long results = userInfoDao.getUserInfoRowCount(assist);
        List<UserInfo> result = userInfoDao.selectUserInfo(assist);
        return new Result(results, result);
    }

    @Override
    public UserInfo findOne(Long id) {
        if (id == null) {
            return null;
        }
        UserInfo result = userInfoDao.selectUserInfoById(id);
        return result;
    }

    @Override
    public Integer saveNotNull(UserInfo value) {
        if (value == null) {
            return null;
        }
        int result = userInfoDao.insertNotNullUserInfo(value);
        return result;
    }

    @Override
    @Transactional
    public Integer updateNotNullById(UserInfo value) {
        if (value == null) {
            return null;
        }
        int result = userInfoDao.updateNotNullUserInfoById(value);
        return result;
    }

    @Override
    public Integer deleteById(Long id) {
        if (id == null) {
            return null;
        }
        int result = userInfoDao.deleteUserInfoById(id);
        return result;
    }

    @Override
    public UserInfo getByExample(UserInfoVO userInfoVO) {
        return userInfoDao.selectUserInfoObjSingle(userInfoVO);
    }

    @Override
    @Transactional
    public Result updateQrcode(Long id, String filePath) {
        return updateQrcodeNew(id, filePath);
    }

    private Result updateQrcodeNew(Long id, String filePath) {
        UserInfo userInfo = userInfoDao.selectUserInfoById(id);
        if (StringUtil.isEmpty(userInfo.getQrcodeUrl())) {
            String forderName = FilePathUtil.getForderNameOSS("QRCODE");
            String realPath = forderName + "/" + IdGenerator.getPrimaryKey() + ".png";
            String cdk = null;
            if (StringUtil.isNotEmpty(userInfo.getQrcode())) {
                cdk = userInfo.getQrcode();
            } else {
                cdk = RC4Util.encry_RC4_string(String.format("%4d", userInfo.getId()), UUID.randomUUID().toString()).toUpperCase();
                userInfo.setQrcode(cdk);
            }
            userInfo.setId(userInfo.getId());
            userInfo.setGmtModified(null);
            boolean success = QRCodeUtil.createCode(300, 300, cdk, filePath + "/" + realPath);
            if (success) {
                userInfo.setQrcodeUrl("upload/" + realPath);
                userInfoDao.updateNotNullUserInfoById(userInfo);
                ossFilesUtil.uploadDocumentByFile(userInfo.getQrcodeUrl(), filePath + "/" + realPath);
                return new Result(Boolean.FALSE, ResultEnum.SUCCESS, userInfo, AppConst.NULL);
            } else {
                return new Result(Boolean.TRUE, ResultEnum.FALSE, AppConst.NULL);
            }
        } else {
            return new Result(Boolean.FALSE, ResultEnum.SUCCESS, userInfo, AppConst.NULL);
        }
    }

    /**
     * 生成链接二维码
     *
     * @param id
     * @param filePath
     * @return
     */
    private Result updateQrcodeOld(Long id, String filePath) {
        String signinFormUrl = "%s/api/signin/toSignin?qrcode=%s";
        UserInfo userInfo = userInfoDao.selectUserInfoById(id);
        if (StringUtil.isEmpty(userInfo.getQrcodeUrl())) {
            String forderName = FilePathUtil.getForderNameOSS("QRCODE");
            String realPath = forderName + "/" + IdGenerator.getPrimaryKey() + ".png";
            String cdk = RC4Util.encry_RC4_string(String.format("%4d", userInfo.getId()), UUID.randomUUID().toString()).toUpperCase();
            userInfo.setId(userInfo.getId());
            userInfo.setQrcode(cdk);
            userInfo.setGmtModified(null);
            String signinUrl = String.format(signinFormUrl, AppConst.FILE_URL, cdk);
            boolean success = QRCodeUtil.createCode(300, 300, signinUrl, filePath + "/" + realPath);
            if (success) {
                userInfo.setQrcodeUrl("upload/" + realPath);
                userInfoDao.updateNotNullUserInfoById(userInfo);
                ossFilesUtil.uploadDocumentByFile(userInfo.getQrcodeUrl(), filePath + "/" + realPath);
                return new Result(Boolean.FALSE, ResultEnum.SUCCESS, userInfo, AppConst.NULL);
            } else {
                return new Result(Boolean.TRUE, ResultEnum.FALSE, AppConst.NULL);
            }
        } else {
            return new Result(Boolean.FALSE, ResultEnum.SUCCESS, userInfo, AppConst.NULL);
        }
    }
}